﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Mime;
using System.Text;
using System.Threading.Tasks;
using Opc.Ua;
using Opc.Ua.Configuration;
using Opc.Ua.Server;
using Quickstarts.ReferenceServer;

namespace ConsoleOpcUaServer
{
    class Program
    {
        static void Main(string[] args)
        {
           RefOpcUaServer myServer= new RefOpcUaServer();
            ApplicationConfiguration  myConfig= CreateAppConfiguration();
            ApplicationInstance application = new ApplicationInstance();
            application.ApplicationType = ApplicationType.Server;
            application.ConfigSectionName = "Quickstarts.ReferenceServer";

            application.LoadApplicationConfiguration(false).Wait();

            bool certOk = application.CheckApplicationInstanceCertificate(false, 0).Result;
            if (!certOk)
            {
                throw new Exception("Application instance certificate invalid!");
            }

            ReferenceServer server = new ReferenceServer();
            application.Start(server).Wait();

            // 创建NodeManager
           // EmptyNodeManager nm =new EmptyNodeManager(server.CurrentInstance, myConfig);
            // // nm.CreateFolder(root, "/MyFolder", "MyFolder");
            //创建地址空间（AddressSpace）
             Opc.Ua.NodeStateCollection nsc=new NodeStateCollection();
              
             using (FileStream fsWrite = new FileStream(@"D:\1.xml", FileMode.Append))
             {
                nsc.SaveAsXml(server.CurrentInstance.DefaultSystemContext, fsWrite);
            };
            
            // nsc.LoadFromXml(server.CurrentInstance.DefaultSystemContext,fs,true);
            // nm.CreateAddressSpace( );
            //myConfig.ApplicationUri
            //  myServer.Start(myConfig);
            Console.ReadKey(true);
        }
        /// <summary>
        /// 创建应用配置（ApplicationConfiguration）
        /// </summary>
        /// <returns></returns>
        public static ApplicationConfiguration CreateAppConfiguration()
        {
            return new Opc.Ua.ApplicationConfiguration()
            {


                ApplicationName = "MyHomework",
                ApplicationUri = Utils.Format(@"urn:{0}:MyHomework", System.Net.Dns.GetHostName()),
                ApplicationType = ApplicationType.Client,
                SecurityConfiguration = new SecurityConfiguration
                {
                    ApplicationCertificate = new CertificateIdentifier
                    {
                        StoreType = @"Directory",
                        StorePath = @"%CommonApplicationData%\OPC Foundation\CertificateStores\MachineDefault",
                        SubjectName = Utils.Format(@"CN={0}, DC={1}", "MyHomework", System.Net.Dns.GetHostName())
                    },
                    TrustedIssuerCertificates = new CertificateTrustList
                    {
                        StoreType = @"Directory",
                        StorePath =
                            @"%CommonApplicationData%\OPC Foundation\CertificateStores\UA Certificate Authorities"
                    },
                    TrustedPeerCertificates = new CertificateTrustList
                    {
                        StoreType = @"Directory",
                        StorePath = @"%CommonApplicationData%\OPC Foundation\CertificateStores\UA Applications"
                    },
                    RejectedCertificateStore = new CertificateTrustList
                    {
                        StoreType = @"Directory",
                        StorePath = @"%CommonApplicationData%\OPC Foundation\CertificateStores\RejectedCertificates"
                    },
                    AutoAcceptUntrustedCertificates = true,
                    AddAppCertToTrustedStore = true
                },
                TransportConfigurations = new TransportConfigurationCollection(),
                TransportQuotas = new TransportQuotas { OperationTimeout = 15000 },
                ClientConfiguration = new ClientConfiguration { DefaultSessionTimeout = 60000 },
                TraceConfiguration = new TraceConfiguration()
            };
        }
    }
}
